<?php
declare (strict_types = 1);

namespace app\model;

use app\BaseModel;

/**
 * CommissionRecord模型
 */
class CommissionRecord extends BaseModel
{
    // 设置表名
    protected $name = 'commission_records';
    
    // 设置字段信息
    protected $schema = [
        'id'               => 'int',
        'user_id'          => 'int',
        'from_user_id'     => 'int',
        'order_id'         => 'string',
        'amount'           => 'float',
        'level'            => 'int',
        'from_level'       => 'int',
        'status'           => 'int',
        'remark'           => 'string',
        'create_time'      => 'datetime',
        'update_time'      => 'datetime',
    ];
    
    // 设置自动时间戳
    protected $autoWriteTimestamp = true;
    
    /**
     * 获取佣金层级文本
     */
    public function getLevelTextAttr()
    {
        $levels = [1 => '一级', 2 => '二级', 3 => '三级及以上'];
        return $levels[$this->level] ?? '未知';
    }
    
    /**
     * 获取来源用户等级文本
     */
    public function getFromLevelTextAttr()
    {
        $levels = [1 => 'V1', 2 => 'V2', 3 => 'V3'];
        return $levels[$this->from_level] ?? '未知';
    }
    
    /**
     * 获取状态文本
     */
    public function getStatusTextAttr()
    {
        $statuses = [0 => '无效', 1 => '有效'];
        return $statuses[$this->status] ?? '未知';
    }
    
    /**
     * 获取佣金所属用户
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
    
    /**
     * 获取佣金来源用户
     */
    public function fromUser()
    {
        return $this->belongsTo(User::class, 'from_user_id', 'id');
    }
} 